import { defineStore } from 'pinia'
import { ref } from 'vue'

export const usetestStore = defineStore(
  'test',
  () => {
    const visiable = ref(false)
    const userInfo = ref(
      JSON.parse(localStorage.getItem('wxLogin') as string) || {},
    )

    const setUserInfo = (info: any) => {
      userInfo.value = info
    }

    // 查询微信扫码的接口
    const queryState = () => {
      // 开启定时器，每隔一段时间问：本地是否有数据
      const tiemr = setInterval(() => {
        const LocUserInfo = JSON.parse(
          localStorage.getItem('wxLogin') as string,
        )
        if (LocUserInfo && LocUserInfo.token) {
          visiable.value = false
          userInfo.value = LocUserInfo
          tiemr && clearInterval(tiemr)
        }
      }, 1000)
    }
    const changeVisiable = (boo: boolean) => {
      visiable.value = boo
    }

    return { userInfo, visiable, setUserInfo, queryState, changeVisiable }
  },
  {
    persist: true,
  },
)
